﻿using System.Linq.Expressions;
using SqlSugar;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace Ln.Fram.SqlSugarCore.Abstractions;

public interface ISqlSugarObjectRepository<TDbContext, TEntity>
	where TDbContext : ISqlSugarDbContext
	where TEntity : class, IEntity, new()
{
	ISqlSugarClient _Db { get; set; }
	ISugarQueryable<TEntity> _DbQueryable { get; }
	Task<ISqlSugarClient> AsSugarClient();
	Task<ITenant> AsTenant();
	Task<ISqlSugarClient> GetDbContextAsync();
	
	#region 插入
	Task<IInsertable<TEntity>> AsInsertable(List<TEntity> insertObjs);
	Task<IInsertable<TEntity>> AsInsertable(TEntity insertObj);
	Task<IInsertable<TEntity>> AsInsertable(TEntity[] insertObjs);
	Task<bool> InsertAsync(TEntity insertObj);
	Task<bool> InsertOrUpdateAsync(TEntity data);
	Task<bool> InsertOrUpdateAsync(List<TEntity> datas);
	Task<int> InsertReturnIdentityAsync(TEntity insertObj);
	Task<long> InsertReturnBigIdentityAsync(TEntity insertObj);
	Task<long> InsertReturnSnowflakeIdAsync(TEntity insertObj);
	Task<TEntity> InsertReturnEntityAsync(TEntity insertObj);
	Task<bool> InsertRangeAsync(List<TEntity> insertObjs);
	#endregion
	
	#region 更新
	Task<IUpdateable<TEntity>> AsUpdateable(List<TEntity> updateObjs);
	Task<IUpdateable<TEntity>> AsUpdateable(TEntity updateObj);
	Task<IUpdateable<TEntity>> AsUpdateable();
	Task<IUpdateable<TEntity>> AsUpdateable(TEntity[] updateObjs);
	Task<bool> UpdateAsync(TEntity updateObj);
	Task<bool> UpdateRangeAsync(List<TEntity> updateObjs);
	Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression);
	#endregion
	
	#region 删除
	Task<IDeleteable<TEntity>> AsDeleteable();
	Task<bool> DeleteAsync(TEntity deleteObj);
	Task<bool> DeleteAsync(List<TEntity> deleteObjs);
	Task<bool> DeleteAsync(Expression<Func<TEntity, bool>> whereExpression);
	Task<bool> DeleteByIdAsync(dynamic id);
	Task<bool> DeleteByIdsAsync(dynamic[] ids);
	#endregion
	
	#region 单查
	Task<ISugarQueryable<TEntity>> AsQueryable();
	Task<TEntity> GetByIdAsync(dynamic id);
	Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> whereExpression);
	Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression);
	Task<bool> IsAnyAsync(Expression<Func<TEntity, bool>> whereExpression);
	Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression);
	#endregion

	#region 多查
	Task<List<TEntity>> GetListAsync();
	Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> whereExpression);
	#endregion

	#region 分页查
	Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize);
	Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<TEntity, object>>? orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
	#endregion
}

public interface ISqlSugarObjectRepository<TDbContext, TEntity, TKey> : ISqlSugarObjectRepository<TDbContext, TEntity>
	where TDbContext : ISqlSugarDbContext
	where TEntity : class, IEntity, new()
{
}

public interface ISqlSugarRepository<TEntity> : ISqlSugarObjectRepository<ISqlSugarDbContext, TEntity>
	where TEntity : class, IEntity, new()
{
}

public interface ISqlSugarRepository<TEntity, TKey> ISqlSugarRepository<ISqlSugarDbContext, TEntity, TKey>
	where TEntity : class, IEntity, new()
{
}
